<?php
namespace Core;

class Config
{
    const SYSTEM_DIR = __DIR__ . '/config/';
    const CACHE_DIR = CACHE_DIR . 'config/';
    const MODULES_DIR = CONFIG_DIR . 'modules/';
    const THEMES_DIR = CONFIG_DIR . 'themes/';

    public function cache($string, $default = [])
    {
        if ($this->cacheExists($string)) {
            $filePath = self::CACHE_DIR . $string . '.php';
            if (file_exists($filePath)) {
                return require $filePath;
            }
        } else {
            $this->createCache($string);
            return $this->cache($string);
        }
        return $default;
    }

    public function cacheExists($name)
    {
        if (file_exists(self::CACHE_DIR . $name . '.php')) {
            return true;
        }
        return false;
    }

    public function isExists($name)
    {
        if ($this->get($name)) {
            return true;
        } else {
            return false;
        }
    }

    public function createCache($name)
    {
        $modules = $this->get('modules', []);
        $themes = $this->get('themes', []);
        $config = [];
        foreach ($modules as $module) {
            $mConfig = $this->get($module . '.' . $name);
            $config = $this->mergeHook($config, $mConfig);
            $cConfig = $this->get('m.' . $module . '.' . $name);
            $config = $this->mergeHook($config, $cConfig);
        }
        foreach ($themes as $module) {
            $mConfig = $this->get('theme.' . $module . '.' . $name);
            $config = $this->mergeHook($config, $mConfig);
            $cConfig = $this->get('t.' . $module . '.' . $name);
            $config = $this->mergeHook($config, $cConfig);
        }
        $filePath = self::CACHE_DIR . $name . '.php';
        $settings = '<?php return ' . var_export($config, true) . ';';
        $output = file_put_contents($filePath, $settings);
        if ($output) {
            return true;
        } else {
            return false;
        }
    }

    public function mergeHook($hook, $add)
    {
        if (is_array($add)) {
            foreach ($add as $key => $value) {
                if (isset($hook[$key])) {
                    $hook[$key] += $value;
                } else {
                    $hook[$key] = $value;
                }
            }
        }
        return $hook;
    }

    public function deleteCache($name)
    {
        if (is_array($name)) {
            foreach ($name as $n) {
                $filePath = self::CACHE_DIR . $n . '.php';
                File::rm($filePath);
            }
        } elseif (is_string($name)) {
            File::rm(self::CACHE_DIR . $name . '.php');
        }
        return true;
    }

    public function clearCache()
    {
        return File::clearDir(self::CACHE_DIR);
    }

    public function delete($config)
    {
        $exConfig = explode('.', $config);
        switch (count($exConfig)) {
            case 1:
                $path = CONFIG_DIR . $exConfig[0] . '.php';
                break;
            case 2:
                $path = MODULES_DIR . ucfirst($exConfig[0]) . '/config/' . $exConfig[1] . '.php';
                break;
            case 3:
                switch ($exConfig[0]) {
                    case 't':
                        $path = self::THEMES_DIR . $exConfig[1] . '/' . $exConfig[2] . '.php';
                        break;
                    case 'm':
                        $path = self::MODULES_DIR . $exConfig[1] . '/' . $exConfig[2] . '.php';
                        break;
                    default:
                        $path = THEMES_DIR . $exConfig[1] . '/config/' . $exConfig[2] . '.php';
                        break;
                }
                break;
            default:
                return false;
                break;
        }
        if (file_exists($path) && unlink($path)) {
            return true;
        } elseif (!file_exists($path)) {
            return true;
        }
        return false;
    }

    public function get($config = false, $default = [])
    {
        if (!$config) {
            return $this->get('config');
        }
        $exConfig = explode('.', $config);
        switch (count($exConfig)) {
            case 1:
                $path = CONFIG_DIR . $exConfig[0] . '.php';
                break;
            case 2:
                $path = MODULES_DIR . ucfirst($exConfig[0]) . '/config/' . $exConfig[1] . '.php';
                break;
            case 3:
                switch ($exConfig[0]) {
                    case 't':
                        $path = self::THEMES_DIR . $exConfig[1] . '/' . $exConfig[2] . '.php';
                        break;
                    case 'm':
                        $path = self::MODULES_DIR . $exConfig[1] . '/' . $exConfig[2] . '.php';
                        break;
                    default:
                        $path = THEMES_DIR . $exConfig[1] . '/config/' . $exConfig[2] . '.php';
                        break;
                }
                break;
            default:
                return $default;
                break;
        }
        if (file_exists($path)) {
            return require $path;
        } else {
            return $default;
        }
    }

    public function set($config, $data)
    {
        $path = false;
        $exConfig = explode('.', $config);
        switch (count($exConfig)) {
            case 1:
                $path = CONFIG_DIR . $exConfig[0] . '.php';
                break;
            case 2:
                $path = MODULES_DIR . ucfirst($exConfig[0]) . '/config/' . $exConfig[1] . '.php';
                break;
            case 3:
                switch ($exConfig[0]) {
                    case 't':
                        $path = self::THEMES_DIR . $exConfig[1] . '/' . $exConfig[2] . '.php';
                        break;
                    case 'm':
                        $path = self::MODULES_DIR . $exConfig[1] . '/' . $exConfig[2] . '.php';
                        break;
                    default:
                        $path = THEMES_DIR . $exConfig[1] . '/config/' . $exConfig[2] . '.php';
                        break;
                }
                break;
        }
        if ($path === false) {
            return false;
        }
        $settings = '<?php return ' . var_export($data, true) . ';';
        $output = file_put_contents($path, $settings);
        if ($output) {
            return true;
        } else {
            return false;
        }
    }

    public function printCode($code, $output = TRUE)
    {
        $codeString = '<pre><h3>打印变量</h3>';
        $codeString .= print_r($code, true);
        $codeString .= '</pre>';
        if ($output) {
            echo $codeString;
        } else {
            return $codeString;
        }
    }
}
